package com.hd.pzhwlxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hd.pzhwlxt.mapper.GoodsMapper;
import com.hd.pzhwlxt.pojo.Goods;
import com.hd.pzhwlxt.pojo.GoodsPrice;
import com.hd.pzhwlxt.service.GoodsPriceService;
import com.hd.pzhwlxt.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
/**
@create: 2022-09-22 11:15
@author: 怀念天空
@Description: 
*/

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService{

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GoodsPriceService goodsPriceService;
    @Override
    public List<Goods> getAllGoodsAndPrice() {
        return goodsMapper.getAllGoodsAndPrice();
    }
    @Override
    public Page<Goods> getAllGoodsAndPricePage(Integer pageNum,Integer pageSize) {
        return goodsMapper.getAllGoodsAndPricePage(new Page<>(pageNum,pageSize));
    }

    @Override
    public Goods getGoodsById(Integer id) {
        return goodsMapper.getGoodsById(id);
    }

    @Override
    public boolean saveGoodsAndPrice(Goods goods) {
        goodsMapper.insert(goods);
        GoodsPrice goodsPrice = goods.getGoodsPrice();
        goodsPrice.setGoodsId(goods.getId());
        return goodsPriceService.save(goodsPrice);
    }

    @Override
    public boolean updateGoods(Goods goods) {
        goodsMapper.updateById(goods);
        GoodsPrice goodsPrice = goods.getGoodsPrice();
        QueryWrapper<GoodsPrice> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id",goods.getId());
        return goodsPriceService.update(goodsPrice,wrapper);
    }
}
